package com.atguigu.bigdata.chapter11.sql;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink02_SQL_File {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 通过ddl方式建表, 直接和文件关联, 将来从这个表读取数据, 就自动从文件读取
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int" +
                            ")with(" +
                            " 'connector' = 'filesystem', " +
                            " 'path' = 'input/sensor.txt', " +
                            " 'format' = 'csv' " +
                            ")");
    
    
        Table result = tEnv.sqlQuery("select * from sensor where id='sensor_1'");
        tEnv.executeSql("create table sensor_out(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int" +
                            ")with(" +
                            " 'connector' = 'filesystem', " +
                            " 'path' = 'input/test', " +
                            " 'format' = 'json' " +
                            ")");
        
        result.executeInsert("sensor_out");
        
    
    }
}
/*
读写文件
    ddl方式
    执行一个建表语句直接和文件进行关联


读写kakfa

 */